/**
 * 爱心照片墙功能实现
 */
;$(function () {
    var count = 0;
    var selectFlag = false;
    var startFlag = false;
    var roundBox;
    // 让爱心墙居中显示
    $("#container").css("width", window.innerHeight * 4 / 3)
        .css("position","fixed")
        .css("left","50%")
        .css("top",48)
        .css("margin-left",-(window.innerHeight * 4 / 3)/2);
    
    // 当屏幕拉伸时去自适应屏幕宽度
    $(window).resize(function(){
        $("#container").css("width", window.innerHeight * 4 / 3)
            .css("margin-left",-(window.innerHeight * 4 / 3)/2);
    });
    
    var appendPic = function (item) {
        if (selectFlag) {
            return
        }
        var x = Math.random() * 4,
            y = Math.random() * 4
        if (!heartFunc(x, y)) {
            appendPic(item)
        } else {
            var back = "jpg";
            var imgNum = 10;
            if (item % imgNum == 1) {
                back = "gif"
            }
            if (item % imgNum == 2 || item % imgNum == 3 || item % imgNum == 4) {
                back = "png"
            }
            var html = '<img src="./images/img' + item % imgNum + '.' + back + '" class="picBase" id="pic-' + item + '"  />'
            $("#container").append(html)
            setTimeout(function () {
                $("#pic-" + item).css("top", 45 + 25 * (2 - y) * 0.85 + "%").css("left", 50 + 25 * (2 - x) * 0.85 + "%")
                    .css("transform", "rotate("+(25 * (0.5 - Math.random())) + "deg) translate(-50%,-50%)")
                    .css("width", "160px").css("height", "160px")
            }, 500);
        }
    }
    
    var init = function () {
        for (var i = 1; i < 50; i++) {
            appendPic(i)
            count++
        }			
    }
    var selectFunc = function () {
        $(".surprise").removeClass("surprise").css("transform", "rotate("+(25 * (0.5 - Math.random())) + "deg) ")
        $("#pic-" + Math.floor(Math.random() * count)).addClass("surprise")		
        $(".start").removeClass("start")		
    }
    var comfirmFunc = function () {				
        startFunc();
    }
    var startFunc = function () {
        $(".surprise").removeClass("surprise")
        roundBox= window.setInterval(function(){
            
        $(".start").removeClass("start")
            for (var i = 1; i < count; i++) {
            if(i%20==Math.floor(Math.random() * 20)){
                $("#pic-" + i).addClass("start")
            }					
        }
        },200)				
    }

    var enterNum=0

    var heartFunc = function (x, y) {
        if (x >= 0 && x < 1) {
            if (y < (x + 3) && y > (-x + 2)) {
                return true
            }
        } else if (x >= 1 && x < 2) {
            if (y < (-x + 5) && y > (-x + 2)) {
                return true
            }
        } else if (x >= 2 && x < 3) {
            if (y < (x + 1) && y > (x - 2)) {
                return true
            }
        } else if (x >= 3 && x < 4) {
            if (y < (-x + 7) && y > (x - 2)) {
                return true
            }
        }
        return false
    }

    init();

    // 鼠标移动到照片上
    $(".picBase").mouseenter(function (){
        $(this).animate({width:"220px","height":"220px","z-index":20},100);
    });

    // 鼠标离开当前的照片
    $(".picBase").mouseleave(function (){
        $(this).animate({width:"160px","height":"160px","z-index":0},100);
    });
})